package com.taxi.comm;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taxi.comm.business.SpringContext;
import com.taxi.comm.server.Configuration;
import com.taxi.comm.server.Server;
import com.taxi.comm.server.mina.MinaServer;

/**
 * An APACHE commons daemon compatible implementation.
 * 
 * @author Watertao
 *
 */
public class Bootstrap {

	private final static Logger log = LoggerFactory.getLogger(Bootstrap.class);
	
	/** The name of default configuration file */
	private final static String CONFIG_FILE_NAME = "pmsfront.xml";
	
	/**
	 *  Here open configuration files, create a trace file, create ServerSockets, Threads
	 * 
	 * @param arguments
	 * @throws Exception 
	 */
	public void init(String[] arguments) throws Exception {
	    log.info("Parsing configuration file.");
		InputStream in = Bootstrap.class.getResourceAsStream("/" + CONFIG_FILE_NAME);
		if (in == null) {
			
			log.error("Cannot find {} in classpath.", CONFIG_FILE_NAME);
			System.exit(0);
		}
		try {
			Configuration.initConfig(in);
		} catch (Exception e) {
			log.error("Error during processing config file", e);
			System.exit(0);
		}
		
		log.info("Configuration file loaded.");
		
		SpringContext.init();
	}
	
	/**
	 *  Start the Thread, accept incoming connections
	 */
	public void start() {
		
		Server server = new MinaServer();
		server.startup();
	}
	
	/**
	 * Inform the Thread to terminate the run(), close the ServerSockets
	 */
	void stop() {
		throw new UnsupportedOperationException();
	}
	
	/**
	 * Destroy any object created in init()
	 */
	void destroy() {
		throw new UnsupportedOperationException();
	}
	
}
